class Solution(object):
    def largestMerge(self, word1, word2):
        n1 = len(word1)
        n2 = len(word2)
        index1 = 0
        index2 = 0
        ans = []
        while index1 < n1 and index2 < n2:
            if word1[index1] < word2[index2]:
                ans.append(word2[index2])
                index2 += 1
            else:
                if word1[index1] > word2[index2]:
                    ans.append(word1[index1])
                    index1 += 1
                else:
                    l_index1 = index1
                    r_index2 = index2
                    while l_index1 < n1 and r_index2 < n2 and word1[l_index1] == word2[r_index2]:
                        l_index1 += 1
                        r_index2 += 1
                    if l_index1 == n1:
                        ans.append(word2[index2])
                        index2 += 1
                    else:
                        if r_index2 == n2:
                            ans.append(word1[index1])
                            index1 += 1
                        else:
                            if word1[l_index1] > word2[r_index2]:
                                ans.append(word1[index1])
                                index1 += 1
                            else:
                                ans.append(word2[index2])
                                index2 += 1
        if index1 < n1:
            ans.append(word1[index1:])
        if index2 < n2:
            ans.append(word2[index2:])
        return ''.join(ans)

